package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;
    @Autowired
    private UserAccountDetailMapper userAccountDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserAccount(Map<String, Object> map) {
        Long userId = (Long)map.get("userId");
        BigDecimal amount = (BigDecimal)map.get("amount");
        String title = (String)map.get("title");
        String orderNo = (String)map.get("orderNo");
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(amount);
        userAccount.setAvailableAmount(amount);
        userAccount.setTotalIncomeAmount(amount);
        userAccountMapper.insert(userAccount);

        this.saveUserAccountDetail(userId, title, SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, amount, orderNo);
    }

    @Override
    public void saveUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {

        UserAccountDetail userAccountDetail = new UserAccountDetail();
        userAccountDetail.setUserId(userId);
        userAccountDetail.setAmount(amount);
        userAccountDetail.setTradeType(tradeType);
        userAccountDetail.setTitle(title);
        userAccountDetail.setOrderNo(orderNo);
        userAccountDetailMapper.insert(userAccountDetail);
    }

    @Override
    public BigDecimal getAvailableAmount(Long userId) {
        LambdaQueryWrapper<UserAccount> userAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userAccountLambdaQueryWrapper.eq(UserAccount::getUserId, userId);
        UserAccount userAccount = baseMapper.selectOne(userAccountLambdaQueryWrapper);
        Assert.notNull(userAccount, "用户账户不存在");
        return userAccount.getAvailableAmount();
    }

    /**
     * 扣减账户余额
     *
     * @param accountDeductVo
     * @return
     */
    @Override
    public void checkAndDeduct(AccountDeductVo accountDeductVo) {
        //1.扣减账户余额
        int rows = userAccountMapper.checkAndDeduct(accountDeductVo);
        if (rows == 0){
            throw new GuiguException(500, "账户余额不足");
        } else {
            this.saveUserAccountDetail(accountDeductVo.getUserId(), accountDeductVo.getContent(),SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,accountDeductVo.getAmount(), accountDeductVo.getOrderNo());
        }

    }

    @Override
    public void getUserRechargePage(Page<UserAccountDetail> pageInfo, String tradeType, Long userId) {
        //1.创建分页查询条件对象
        LambdaQueryWrapper<UserAccountDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAccountDetail::getUserId, userId);
        queryWrapper.eq(UserAccountDetail::getTradeType, tradeType);
        queryWrapper.select(UserAccountDetail::getOrderNo, UserAccountDetail::getTradeType, UserAccountDetail::getTitle, UserAccountDetail::getAmount);
        userAccountDetailMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public void getUserAccountDetailPage(Page<UserAccountDetail> pageInfo, String tradeType, Long userId) {
//1.创建分页查询条件对象
        LambdaQueryWrapper<UserAccountDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserAccountDetail::getUserId, userId);
        queryWrapper.eq(UserAccountDetail::getTradeType, tradeType);
        queryWrapper.select(UserAccountDetail::getOrderNo, UserAccountDetail::getTradeType, UserAccountDetail::getTitle, UserAccountDetail::getAmount);
        queryWrapper.orderByDesc(UserAccountDetail::getId);
        userAccountDetailMapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public void saveUserAccountOrderDetail(UserAccountDetail userAccountDetail) {
        userAccountDetailMapper.insert(userAccountDetail);
    }
}
